package org.test.bookpub;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.scheduling.annotation.Scheduled;
import org.test.bookpub.entity.Author;
import org.test.bookpub.entity.Book;
import org.test.bookpub.entity.Publisher;
import org.test.bookpub.entity.Teammate;
import org.test.bookpub.repository.AuthorRepository;
import org.test.bookpub.repository.BookRepository;
import org.test.bookpub.repository.PublisherRepository;
import org.test.bookpub.repository.TeammateRepository;

/**
 * Created by Bai on 2016/5/19/0019.
 * 这个类的作用主要是在application启动时，做一些事情。
 * 需要作为一个@Bean在bootApplication里面注册
 */
public class StartupRunner implements CommandLineRunner {
    protected final Log logger = LogFactory.getLog(getClass());
    @Autowired
    private BookRepository bookRepository;
    @Autowired
    private AuthorRepository authorRepository;
    @Autowired
    private PublisherRepository publisherRepository;

    @Autowired
    TeammateRepository teammateRepository;

    @Override
    public void run(String... args) throws Exception {
        logger.info("Number of books: " + bookRepository.count());
        logger.info("teammateRepository.count():"+teammateRepository.count());
    }

    //定时器
   @Scheduled(initialDelay = 1000, fixedRate = 10000)
    public void run() {

        for (Teammate teammate : teammateRepository.findAll()) {
            logger.info(teammate.toString());
        }
    }
}

